package com.onluy.sysman.controller.sysmgr;


import com.alibaba.fastjson.JSON;
import com.onluy.frameworkcloud.Param.ParamPagination;
import com.onluy.frameworkcloud.Param.ReqHead;
import com.onluy.frameworkcloud.Param.ReqInfo;
import com.onluy.frameworkcloud.Param.RspInfo;
import com.onluy.frameworkcloud.base.AppWalkController;
import com.onluy.frameworkcloud.data.bean.PageData;
import com.onluy.frameworkcloud.data.util.InParam;
import com.onluy.sysman.param.AuthFunParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import com.onluy.sysman.service.TdAuthFunService;
import com.onluy.sysman.entity.sysmgr.TdAuthFun;

@Validated
@RestController
@RequestMapping("/authfun")
@Api(value="authfun", description="/功能权限管理接口", position=1 )
public class TdAuthFunController extends AppWalkController {


	//调用服务
	@Autowired
	private TdAuthFunService tdAuthFunService;

	@RequestMapping(value={"/target"},method={ RequestMethod.POST})
	@ApiOperation(value="目标", notes="根据 departCode  获取信息")
	public RspInfo<TdAuthFun> dotarget(@Valid @RequestBody ReqInfo<ReqHead, AuthFunParam> reqInfo   ) throws Exception {
		RspInfo<TdAuthFun> jsonResult = new RspInfo<TdAuthFun>();
		log.info("------------>Model reqInfo : " + JSON.toJSONString(reqInfo));
		TdAuthFun tdAuthFun = new TdAuthFun();

		//if (null !=  reqInfo.getReqBody().getDepartCode()) {
			//tdAuthFun.setDepartCode(reqInfo.getReqBody().getDepartCode()).asCondition();
		//}
		tdAuthFun.setAreaCode(reqInfo.getReqBody().getAreaCode()).asCondition();
		tdAuthFun.setSysCode(reqInfo.getReqBody().getSysCode()).asCondition();
		tdAuthFun.setFunCode(reqInfo.getReqBody().getFunCode()).asCondition();

		tdAuthFun = tdAuthFunService.model(tdAuthFun);
		jsonResult.setRspCode(0);
		jsonResult.setRspData(tdAuthFun);
		return jsonResult;
	}



	@RequestMapping(value={ "/add"},method={RequestMethod.POST })
	@ApiOperation(value="新增", notes="新增")
	public RspInfo<InParam> doAdd(@RequestBody @Valid ReqInfo<ReqHead,TdAuthFun> reqInfo )  throws Exception{
		RspInfo<InParam> jsonResult = new RspInfo<InParam>();
		int num = 0;
		TdAuthFun tdAuthFun = reqInfo.getReqBody();
		num = tdAuthFunService.add(tdAuthFun);
		if (num > 0) {
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("增加成功！");
		} else {
			jsonResult.setRspDesc("增加失败！");
		}
		return jsonResult;
	}

	@RequestMapping(value={ "/update"},method={RequestMethod.POST})
	@ApiOperation(value="更新", notes="根据 areaCode,sysCode,funCode 更新")
	public RspInfo<InParam> doUpdate(      @RequestBody @Valid  ReqInfo<ReqHead,TdAuthFun> reqInfo ) throws Exception {
		RspInfo<InParam> jsonResult=new RspInfo<InParam>();
		int num=0;
		TdAuthFun tdAuthFun = reqInfo.getReqBody();


		tdAuthFun.setAreaCode(reqInfo.getReqBody().getAreaCode()).asCondition();
		tdAuthFun.setSysCode(reqInfo.getReqBody().getSysCode()).asCondition();
		tdAuthFun.setFunCode(reqInfo.getReqBody().getFunCode()).asCondition();


		num=tdAuthFunService.update( tdAuthFun);
		if(  num>0 ){
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("修改成功！");
		}
		else {
			jsonResult.setRspDesc("修改失败！");
		}
		return jsonResult;
	}




	@RequestMapping(value={"/del"},method={RequestMethod.POST})
	@ApiOperation(value="删除", notes="根据  areaCode,sysCode,funCode  删除")
	public RspInfo<InParam> doDel( @RequestBody    ReqInfo<ReqHead,AuthFunParam> reqInfo) throws Exception {
		RspInfo<InParam> jsonResult=new RspInfo<InParam>();
		TdAuthFun tdAuthFun = new TdAuthFun();
		int num=1;
		try {
			if( null == reqInfo.getReqBody().getAreaCode() )throw new Exception("缺少必要属性 areaCode ！");
			if( null == reqInfo.getReqBody().getSysCode() )throw new Exception("缺少必要属性 sysCode ！");
			if( null == reqInfo.getReqBody().getFunCode() )throw new Exception("缺少必要属性 funCode ！");

			tdAuthFun.setAreaCode(reqInfo.getReqBody().getAreaCode()).asCondition();
			tdAuthFun.setSysCode(reqInfo.getReqBody().getSysCode()).asCondition();
			tdAuthFun.setFunCode(reqInfo.getReqBody().getFunCode()).asCondition();


			num=tdAuthFunService.del( tdAuthFun);
		} catch (Exception e) {
			jsonResult.setRspDesc("删除异常:"+e.getMessage());
			e.printStackTrace();
		}
		if(  num>0 ){
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("删除成功！");
		}
		else {
			jsonResult.setRspDesc("删除失败！");
		}
		return jsonResult;
	}


	@ResponseBody
	@RequestMapping(value={ "/list"},method={RequestMethod.POST })
	@ApiOperation(value="列表", notes="列表明细")
	public RspInfo<PageData<TdAuthFun>>  doList(@RequestBody    ReqInfo<ParamPagination, TdAuthFun> reqInfo )throws Exception {
		RspInfo<PageData<TdAuthFun>> jsonResult=new RspInfo<PageData<TdAuthFun>>();

		this.MakeLikeEntity(  reqInfo.getReqBody());//处理查询条件

		PageData<TdAuthFun> list =tdAuthFunService.list( reqInfo.getReqBody(),FormatPagination(reqInfo.getReqHead()));
		jsonResult.setRspCode(0);
		jsonResult.setRspData(list);
		return jsonResult;
	}

	public  void  MakeLikeEntity(TdAuthFun tdAuthFun){

		if(tdAuthFun == null )return;

		if(null!=tdAuthFun.getAreaCode()){
			tdAuthFun.setAreaCode(tdAuthFun.getAreaCode()).asCondition();
		}if(null!=tdAuthFun.getSysCode()){
			tdAuthFun.setSysCode(tdAuthFun.getSysCode()).asCondition();
		}if(null!=tdAuthFun.getFunCode()){
			tdAuthFun.setFunCode(tdAuthFun.getFunCode()).asCondition();
		}
		if(null!=tdAuthFun.getFunState()){
			tdAuthFun.setFunState(tdAuthFun.getFunState()).asCondition();
		}

		if(null!=tdAuthFun.getFunType()){
			tdAuthFun.setFunType(tdAuthFun.getFunType()).asCondition();
		}
		if(null!=tdAuthFun.getFunName()){
			tdAuthFun.setFunName(null).getCondition().andLike( "%"+tdAuthFun.getFunName()+"%" );
		}
		if(null!=tdAuthFun.getFunMemo()){
			tdAuthFun.setFunMemo(null).getCondition().andLike( "%"+tdAuthFun.getFunMemo()+"%" );
		}
	}


}
